// xhr请求 (ALL)
export function request({url,method,data = {},param = {},header = {}}){
	return new Promise((resolve,reject)=>{
		const paramKeys = Object.keys(param)
		let paramStr = ''
		if(paramKeys.length){
			paramKeys += '?'
			paramKeys.forEach((el,idx)=>{
				paramKeys += `${el}=${param[el]}${idx === paramKeys.length-1?'':'&'}`
			})
		}
		uni.request({
			url:getApp().globalData.baseUrl + url + paramStr,
			timeout:30000,
			method,
			data,
			header,
			success:(res)=>{
				resolve(res)
			},
			fail:(err)=>{
				reject(err)
			}
		})
	})
}

// 获取服务供应商 (小程序、APP)
// 参数及返回值说明请见 https://uniapp.dcloud.io/api/plugins/provider?id=getprovider
export function getProvider({service}){
	return new Promise((resolve,reject)=>{
		uni.getProvider({
			service,
			success:(res)=>{
				resolve(res)
			},
			fail:(err)=>{
				reject(err)
			}
		})
	})
}

// 登录 (小程序、APP)
// 参数及返回值说明请见 https://uniapp.dcloud.io/api/plugins/login?id=login
export function login({provider,scopes = 'auth_base',univerifyStyle = {},onlyAuthorize = true}){
	return new Promise((resolve,reject)=>{
		uni.login({
			provider,
			scopes,
			timeout:30000,
			univerifyStyle,
			onlyAuthorize,
			success:(res)=>{
				resolve(res)
			},
			fail:(err)=>{
				reject(err)
			}
		})
	})
}

// 检查登录状态是否过期 (小程序、APP)
// 参数及返回值说明请见 https://uniapp.dcloud.io/api/plugins/login?id=unichecksession
export function checkSession(){
	return new Promise((resolve,reject)=>{
		uni.checkSession({
			success:()=>{
				resolve()
			},
			fail:()=>{
				reject(null)
			}
		})
	})
}

// 获取用户信息 (小程序、APP)
// 参数及返回值说明请见 https://uniapp.dcloud.io/api/plugins/login?id=getuserinfo
export function getUserInfo({provider,withCredentials = true}){
	return new Promise((resolve,reject)=>{
		uni.getUserInfo({
			provider,
			withCredentials,
			timeout:30000,
			success:(res)=>{
				resolve(res)
			},
			reject:(err)=>{
				reject(err)
			}
		})
	})
}

// 获取用户信息弹窗 (小程序)
// 参数及返回值说明请见 https://uniapp.dcloud.io/api/plugins/login?id=getuserprofile
export function getUserProfile({desc}){
	return new Promise((resolve,reject)=>{
		uni.getUserProfile({
			desc,
			success:(res)=>{
				resolve(res)
			},
			reject:(err)=>{
				reject(err)
			}
		})
	})
}